<?php
class adminLogin extends divForm
{
    public function __construct()
    {
        global $login;
        global $page;

        //Delete
        if($deleteAction = getActionResult('divAdmin','remove') );
        {
            $login->removeUser($deleteAction);
        }

        $page->addJsCode("
                        function validate_form(thisform)
                        {
                            var login    = validatorPost('login'   , '10', true);
                            var passwd   = validatorPost('passwd'  , '10', true);
                            var username = validatorPost('username', '0' , true);
                            return (login && passwd && username);
                        }

                        function validate_grid(thisform)
                        {
                            var answer = confirm('Tem certeza que deseja efetuar a operacao?')

                            if (answer)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    ");

        //Form Mount
        $columns = array('login', 'username', 'email', 'class', 'count', 'lastIp');
        $widths = array(100,200, 200, 50, 50, 80);

        $iLogin     =  new textField('login',  null, 'Login', 100, 90, 20, 20, 30, 'text');
        $iLogin     -> getField()->addEvent('onkeypress', "validatorKeyPress(this, event, '10')");
        $iLogin     -> getField()->addEvent('onblur', "validatorBlur(this, '10', true)");

        $tableAction = getActionResult('divAdmin');

        if ($tableAction)
        {
                $result = $login->getUserData($tableAction);

                foreach ($result as $row)
                {

                    $loginValue     = $tableAction;
                    $usernameValue  = $row['username'];
                    $emailValue     = $row['email'];
                    $passwordValue  = '';
                    $classValue     = $row['class'];
                }
        }
        else
        {
                $loginValue     = $_POST['login'];
                $usernameValue  = $_POST['username'];
                $emailValue     = $_POST['email'];
                $passwordValue  = $_POST['passwd'];
                $classValue     = $_POST['class'];
        }

        $iLogin     ->getField()->setValue( $loginValue );

        if ($tableAction)
        {
            $iLogin->getField()->addAttribute('readonly', 'readonly');
        }

        $iUsername  = new textField('username',  null, 'Nome', 100, 300, 20, 230,30, 'text');
        $iUsername  ->getField()->setValue($usernameValue);

        $iClass     = new SelectField('class',  array('admin'), 'Classe', 100, 90, 20, 20,60, 'text');
        $iClass     ->getField()->setValue($classValue);

        $iEmail     =  new textField('email',  null, 'E-mail', 100, 300, 20, 230,60, 'text');
        $iEmail     -> getField()->setValue($emailValue);

        $ipwd       = new textField('passwd',  'password', 'Senha', 100, 90, 20, 20,90,
     'text');
        $ipwd     ->getField()->addEvent('onkeypress', "validatorKeyPress(this, event, '10')");
        $ipwd     ->getField()->addEvent('onblur', "validatorBlur(this, '10')");

        $ipwd       -> getField()->setValue($passwordValue);

        $iSave      = new submit('save',  'Salvar', 'columnTitle');
        $iSave      -> setBounds(330,90, 90,30);
        $iSave      -> onClick('saveUser');

        $list = $login->listAll();
        $temp = new Grid('divAdmin', $columns, $list, $widths, 20,130, 24, 'columnTitle', 'text');
        $divAdmin = $temp;

        $width = $divAdmin->getStyle()->getValue('height');

        $loginForm = new form('formUser', array(    $iLogin,
                                                    $iUsername,
                                                    $iClass,
                                                    $iEmail,
                                                    $ipwd,
                                                    $iSave
                                                ), 'index.php', 'post', 'return validate_form()');

        $divAdminForm = new form('formUser', array($divAdmin), 'index.php', 'post', 'return validate_grid()');

        parent::__construct('divUser',  array($loginForm,$divAdminForm ), 800,$width+150, "Usuarios") ;

        $this->setPosition(20,60);

        $this->getBar()->class = 'columnTitle';
        $this->class = 'text';
    }
}


/**
 * Called when user choose to save the data in form...
 */
function saveUser($data=null)
{
    global $login;
    global $page;
    if ( $login->createUser($_POST) )
    {
        $page->addJsCode('alert(\'Operacao efetuada com Sucesso!\')');
    }
    else
    {
        $page->addJsCode('alert(\'Falha na Operacao!\');');
    }
}
?>